package com.supercube.wechat.controller;

import com.google.gson.Gson;
import com.supercube.common.utils.MD5Utils;
import com.supercube.common.utils.R;
import com.supercube.common.utils.StringUtils;
import com.supercube.system.domain.UserDO;
import com.supercube.system.service.UserService;
import com.supercube.wechat.domain.WeChatUserInfo;
import com.supercube.wechat.service.WxService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 小程序用户相关的控制器
 *
 * @author djzhao
 * @date 19/10/02 17:05
 * @email djzhao627@gmail.com
 */
@Controller
@RequestMapping("/miniapp")
public class MiniAppUserController {

    @Autowired
    private WxService wxService;

    @Autowired
    UserService userService;

    private Logger logger = LoggerFactory.getLogger(MiniAppUserController.class);

    @ResponseBody
    @PostMapping("/wxAuth")
    public R wxAuth(String code) {
        if (StringUtils.isBlank(code)) {
            return R.error("需要openID");
        }
        return wxService.getWxSession(code);
    }

    /**
     * 小程序登录认证,需要解密数据
     *
     * @param code
     * @param encryptedData
     * @param iv
     * @return
     */
    @ResponseBody
    @PostMapping("/wxAuthWithDecode")
    public R wxAuthWithDecode(String code, String encryptedData, String iv) {
        if (StringUtils.isBlank(code)) {
            return R.error("需要code");
        }
        if (StringUtils.isBlank(encryptedData)) {
            return R.error("需要encryptedData");
        }
        if (StringUtils.isBlank(iv)) {
            return R.error("需要iv");
        }
        return wxService.getWxSession(code, encryptedData, iv);
    }

    /**
     * 注册成为新的用户
     *
     * @param userInfo 微信用户信息
     * @return 新用户信息
     */
    @ResponseBody
    @PostMapping("/register")
    public R registerAsNewUser(String userInfo, String code, String encryptedData, String iv) {
        if (StringUtils.isBlank(code)) {
            return R.error("需要code");
        }
        if (StringUtils.isBlank(encryptedData)) {
            return R.error("需要encryptedData");
        }
        if (StringUtils.isBlank(iv)) {
            return R.error("需要iv");
        }
        if (StringUtils.isBlank(userInfo)) {
            return R.error("用户信息不完整");
        }
        WeChatUserInfo weChatUserInfo = new Gson().fromJson(userInfo, WeChatUserInfo.class);
        return wxService.registerAsNewUser(weChatUserInfo, code, encryptedData, iv);
    }

    @ResponseBody
    @PostMapping("/employeeLogin")
    public R memberLogin(String username, String password) {
        password = MD5Utils.encrypt(username, password);
        // 查询用户信息
        Map<String, Object> map = new HashMap<>();
        map.put("username",username);
        map.put("password",password);
        List<UserDO> sysUserList = userService.list(map);
        if (sysUserList == null || sysUserList.size() == 0) {
            return R.error(1, "用户名或者密码不正确！");
        }
        Map<String, Object> data = new HashMap<>();
        sysUserList.get(0).setPassword(null);
        data.put("user",sysUserList.get(0));
        return R.ok(data);
    }
}